package cc.mrbird.febs.payment.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.payment.entity.PaymentInfo;
import cc.mrbird.febs.payment.service.IPaymentInfoService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 支付记录 Controller
 *
 * @author Mr.Yang
 * @date 2022-08-11 13:50:49
 */
@Slf4j
@Validated
@Controller
public class PaymentInfoController extends BaseController {

    @Autowired
    private IPaymentInfoService paymentInfoService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "paymentInfo")
    public String paymentInfoIndex(){
        return FebsUtil.view("paymentInfo/paymentInfo");
    }

    @GetMapping("paymentInfo")
    @ResponseBody
    @RequiresPermissions("paymentInfo:list")
    public FebsResponse getAllPaymentInfos(PaymentInfo paymentInfo) {
        return new FebsResponse().success().data(paymentInfoService.findPaymentInfos(paymentInfo));
    }

    @GetMapping("paymentInfo/list")
    @ResponseBody
    @RequiresPermissions("paymentInfo:list")
    public FebsResponse paymentInfoList(QueryRequest request, PaymentInfo paymentInfo) {
        Map<String, Object> dataTable = getDataTable(this.paymentInfoService.findPaymentInfos(request, paymentInfo));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增PaymentInfo", exceptionMessage = "新增PaymentInfo失败")
    @PostMapping("paymentInfo")
    @ResponseBody
    @RequiresPermissions("paymentInfo:add")
    public FebsResponse addPaymentInfo(@Valid PaymentInfo paymentInfo) {
        this.paymentInfoService.createPaymentInfo(paymentInfo);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除PaymentInfo", exceptionMessage = "删除PaymentInfo失败")
    @GetMapping("paymentInfo/delete")
    @ResponseBody
    @RequiresPermissions("paymentInfo:delete")
    public FebsResponse deletePaymentInfo(PaymentInfo paymentInfo) {
        this.paymentInfoService.deletePaymentInfo(paymentInfo);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改PaymentInfo", exceptionMessage = "修改PaymentInfo失败")
    @PostMapping("paymentInfo/update")
    @ResponseBody
    @RequiresPermissions("paymentInfo:update")
    public FebsResponse updatePaymentInfo(PaymentInfo paymentInfo) {
        this.paymentInfoService.updatePaymentInfo(paymentInfo);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改PaymentInfo", exceptionMessage = "导出Excel失败")
    @PostMapping("paymentInfo/excel")
    @ResponseBody
    @RequiresPermissions("paymentInfo:export")
    public void export(QueryRequest queryRequest, PaymentInfo paymentInfo, HttpServletResponse response) {
        List<PaymentInfo> paymentInfos = this.paymentInfoService.findPaymentInfos(queryRequest, paymentInfo).getRecords();
        ExcelKit.$Export(PaymentInfo.class, response).downXlsx(paymentInfos, false);
    }
}
